package jnpf.task;

import jnpf.base.UserInfo;
import jnpf.entity.ZXGL_RenWuFanKuiEntity;
import jnpf.entity.ZXGL_XiangMuFanKuiEntity;
import jnpf.entity.ZXGL_XiangMuFenJie_mingxibiaoEntity;
import jnpf.entity.ZXGL_XiangMuLiXiang_MingXiEntity;
import jnpf.permission.UsersApi;
import jnpf.permission.entity.UserEntity;
import jnpf.permission.service.UserService;
import jnpf.service.*;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ZXGL_XiangMuLiXiang_MingXiTask {

    private static final Logger logger = LoggerFactory.getLogger(ZXGL_XiangMuLiXiang_MingXiTask.class);

    @Autowired
    private ZXGL_XiangMuLiXiang_MingXiService zxglXiangMuLiXiangMingXiService;

    @Autowired
    private ZXGL_XiangMuFanKuiService zxglXiangMuFanKuiService;

    @Autowired
    private ZXGL_XiangMuFanKuiService zxgl_xiangMuFanKuiService;

    @Autowired
    private ZXGL_XiangMuFenJie_mingxibiaoService zxglXiangMuFenJieMingxibiaoService;

    @Autowired
    private ZXGL_RenWuFanKuiService zxgl_renWuFanKuiService;

    @Autowired
    private UsersApi usersApi;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private UserService userService;


    /**
     * 每天2点更改当天推送数据推送状态
     * 将计划开始时间为当天的项目，状态改为执行中
     */
    // @Scheduled(cron = "0 0 2 * * ?")
    @Scheduled(fixedDelay = 1 * 60 * 1000)   //每隔1分钟执行
    public void execute() {
        List<ZXGL_XiangMuLiXiang_MingXiEntity> list = zxglXiangMuLiXiangMingXiService.getTodayList();
        for (ZXGL_XiangMuLiXiang_MingXiEntity entity : list) {
            entity.setZt("667276298107753797");
            entity.setSjksrq(entity.getJhkssj());
        }
        zxglXiangMuLiXiangMingXiService.updateBatchById(list);


        // 定时任务判断，根据项目计划开始日期、任务计划开始日期判断，状态自动变更为进行中，
        //同时将计划开始日期赋值给实际开始日期；
        //在计划完成日期前5天将未完成项目或任务状态变更为预警，超过计划完成日期后变更为超期，超过计划完成日期后5天变更为严重超期

        list = zxglXiangMuLiXiangMingXiService.getUnfinishedBefore5Day();
        Date now = new Date();
        for (ZXGL_XiangMuLiXiang_MingXiEntity entity : list) {
            // 计算时间差（毫秒）
            long diffInMillis = entity.getJhjssj().getTime() - now.getTime();
            logger.info("diffInMillis:===============>" + diffInMillis);
            // 转换为其他单位
            long diffInSeconds = diffInMillis / 1000;
            long diffInMinutes = diffInSeconds / 60;
            long diffInHours = diffInMinutes / 60;
            long diffInDays = diffInHours / 24;
            logger.info("diffInDays:===============>" + diffInDays);
            if (diffInDays > 0) {
                // 提前5天标记预警
                entity.setYjbj("667376667227854149");
            } else if (diffInDays <= 0 && diffInDays >= -5) {
                // 超期五天之内
                entity.setYjbj("667376765865301317");
            } else if (diffInDays < -5) {
                // 超期五天之外
                entity.setYjbj("667376817690121541");
            }
        }
        logger.info("list:===============>" + list);
        zxglXiangMuLiXiangMingXiService.updateBatchById(list);


        List<String> yjbjs = new ArrayList<>();
        yjbjs.add("667376667227854149");
        yjbjs.add("667376765865301317");
        yjbjs.add("667376817690121541");
        List<ZXGL_XiangMuLiXiang_MingXiEntity> overdueList = zxglXiangMuLiXiangMingXiService.getOverdueList(yjbjs);
        if (overdueList != null && overdueList.size() > 0) {
            for (ZXGL_XiangMuLiXiang_MingXiEntity entity : overdueList) {
                // 计算时间差（毫秒）
                long diffInMillis = entity.getJhjssj().getTime() - now.getTime();

                // 转换为其他单位
                long diffInSeconds = diffInMillis / 1000;
                long diffInMinutes = diffInSeconds / 60;
                long diffInHours = diffInMinutes / 60;
                long diffInDays = diffInHours / 24;

                // 超出五天
                if (diffInDays > 5) {
                    entity.setYjbj("");
                } else if (diffInDays > 0) {
                    // 提前5天标记预警
                    entity.setYjbj("667376667227854149");
                } else if (diffInDays <= 0 && diffInDays >= -5) {
                    // 超期五天之内
                    entity.setYjbj("667376765865301317");
                } else if (diffInDays < -5) {
                    // 超期五天之外
                    entity.setYjbj("667376817690121541");
                }
            }
        }
        zxglXiangMuLiXiangMingXiService.updateBatchById(overdueList);
    }

    /**
     * 每天晚上十一点发起督办
     */
    // @Scheduled(cron = "0 0 23 * * ?")
    // @Scheduled(fixedDelay = 1 * 60 * 1000)   //每隔1分钟执行
    @Scheduled(cron = "${dsfk.day}")
    public void executeFanKuiDay() {
        List<ZXGL_XiangMuLiXiang_MingXiEntity> fankui = zxglXiangMuLiXiangMingXiService.getFankuiByDate();
        logger.info("fankui:=============================>" + fankui);
        //项目反馈
        for (ZXGL_XiangMuLiXiang_MingXiEntity mingXiEntity : fankui) {
            ZXGL_XiangMuFanKuiEntity entity = zxglXiangMuFanKuiService.addProjectFk(mingXiEntity.getId());
            // ZXGL_XiangMuFanKuiCrForm entity1 = JsonUtil.getJsonToBean(entity, ZXGL_XiangMuFanKuiCrForm.class);

            UserEntity userEntity = usersApi.getInfoById(entity.getZrr());
            if (userEntity != null) {
                UserInfo userInfo = userService.copyFromEntity(userEntity);
                entity.setOrganizeName(userInfo.getOrganizeName());
                entity.setOrganizeId(userInfo.getOrganizeId());
                entity.setCreatorUser(userInfo.getUserName());
                entity.setCreatorUserId(userInfo.getUserId());
                entity.setCompanyId(userInfo.getCompanyId());
                entity.setCompanyName(userInfo.getCompanyName());
                entity.setDepartmentId(userInfo.getDepartmentId());
                entity.setDepartmentName(userInfo.getDepartmentName());
                entity.setCreatorTime(new Date());
                //生成项目反馈
                entity.setId(RandomUtil.uuId());
                zxgl_xiangMuFanKuiService.add(entity);
                logger.info("id:==================>" + entity.getId());
            }

            /**
             * 任务反馈
             */
            //获取任务记录
            List<ZXGL_XiangMuFenJie_mingxibiaoEntity> mingxibiaolist = zxglXiangMuFenJieMingxibiaoService.getListByXmId(mingXiEntity.getId());
            logger.info("mingxibiaolist:==================>" + mingxibiaolist);
            for (ZXGL_XiangMuFenJie_mingxibiaoEntity mx : mingxibiaolist) {
                ZXGL_RenWuFanKuiEntity mingxibiao = zxglXiangMuFenJieMingxibiaoService.createEntity(mx.getId());
                logger.info("mingxibiao:==================>" + mingxibiao);
                // ZXGL_RenWuFanKuiCrForm renWuFanKuiCrForm = JsonUtil.getJsonToBean(mingxibiao, ZXGL_RenWuFanKuiCrForm.class);
                userEntity = usersApi.getInfoById(mingxibiao.getFkr());
                if (userEntity != null) {
                    UserInfo userInfo = userService.copyFromEntity(userEntity);
                    mingxibiao.setOrganizeName(userInfo.getOrganizeName());
                    mingxibiao.setOrganizeId(userInfo.getOrganizeId());
                    mingxibiao.setCreatorUser(userInfo.getUserName());
                    mingxibiao.setCreatorUserId(userInfo.getUserId());
                    mingxibiao.setCompanyId(userInfo.getCompanyId());
                    mingxibiao.setCompanyName(userInfo.getCompanyName());
                    mingxibiao.setDepartmentId(userInfo.getDepartmentId());
                    mingxibiao.setDepartmentName(userInfo.getDepartmentName());
                    mingxibiao.setCreatorTime(new Date());
                    //生成项目反馈
                    mingxibiao.setId(RandomUtil.uuId());
                    zxgl_renWuFanKuiService.add(mingxibiao);
                    logger.info("id:==================>" + mingxibiao.getId());
                }

            }
        }
    }

    /**
     * 每周周三十一点自动发起督办
     */
    // @Scheduled(cron = "0 0 23 * * WED")
    // @Scheduled(fixedDelay = 1 * 60 * 1000)   //每隔1分钟执行
    @Scheduled(cron = "${dsfk.week}")
    public void fanKuiWeek() {
        List<ZXGL_XiangMuLiXiang_MingXiEntity> fankui = zxglXiangMuLiXiangMingXiService.getFankuiByWeek();
        logger.info("fankui:=============================>" + fankui);
        //根据反馈频率进行分组
        for (ZXGL_XiangMuLiXiang_MingXiEntity mingXiEntity : fankui) {
            ZXGL_XiangMuFanKuiEntity entity = zxglXiangMuFanKuiService.addProjectFk(mingXiEntity.getId());

            UserEntity userEntity = usersApi.getInfoById(entity.getZrr());
            if (userEntity != null) {
                UserInfo userInfo = userService.copyFromEntity(userEntity);
                entity.setOrganizeName(userInfo.getOrganizeName());
                entity.setOrganizeId(userInfo.getOrganizeId());
                entity.setCreatorUser(userInfo.getUserName());
                entity.setCreatorUserId(userInfo.getUserId());
                entity.setCompanyId(userInfo.getCompanyId());
                entity.setCompanyName(userInfo.getCompanyName());
                entity.setDepartmentId(userInfo.getDepartmentId());
                entity.setDepartmentName(userInfo.getDepartmentName());
                entity.setCreatorTime(new Date());
                //生成项目反馈
                entity.setId(RandomUtil.uuId());
                zxgl_xiangMuFanKuiService.add(entity);
            }

            /**
             * 任务反馈
             */
            //获取任务记录
            List<ZXGL_XiangMuFenJie_mingxibiaoEntity> mingxibiaolist = zxglXiangMuFenJieMingxibiaoService.getListByXmId(mingXiEntity.getId());
            for (ZXGL_XiangMuFenJie_mingxibiaoEntity mx : mingxibiaolist) {
                ZXGL_RenWuFanKuiEntity mingxibiao = zxglXiangMuFenJieMingxibiaoService.createEntity(mx.getId());
                userEntity = usersApi.getInfoById(mingxibiao.getFkr());
                if (userEntity != null) {
                    UserInfo userInfo = userService.copyFromEntity(userEntity);
                    mingxibiao.setOrganizeName(userInfo.getOrganizeName());
                    mingxibiao.setOrganizeId(userInfo.getOrganizeId());
                    mingxibiao.setCreatorUser(userInfo.getUserName());
                    mingxibiao.setCreatorUserId(userInfo.getUserId());
                    mingxibiao.setCompanyId(userInfo.getCompanyId());
                    mingxibiao.setCompanyName(userInfo.getCompanyName());
                    mingxibiao.setDepartmentId(userInfo.getDepartmentId());
                    mingxibiao.setDepartmentName(userInfo.getDepartmentName());
                    mingxibiao.setCreatorTime(new Date());
                    //生成项目反馈
                    mingxibiao.setId(RandomUtil.uuId());
                    zxgl_renWuFanKuiService.add(mingxibiao);
                }
            }
        }
    }

    /**
     * 每月倒数第三天十一点发起反馈
     */
    // @Scheduled(cron = "0 0 23 L-3 * ?") // 每月倒数第三天 23:00 执行
    // @Scheduled(fixedDelay = 1 * 60 * 1000)   //每隔1分钟执行
    @Scheduled(cron = "${dsfk.month}")
    public void fanKuiMonth() {
        LocalDate today = LocalDate.now();
        LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
        LocalDate thirdLastDayOfMonth = lastDayOfMonth.minusDays(3);
        if (today.isEqual(thirdLastDayOfMonth)) {
            List<ZXGL_XiangMuLiXiang_MingXiEntity> fankui = zxglXiangMuLiXiangMingXiService.getFankuiByMonth();
            logger.info("fankui:=============================>" + fankui);
            //根据反馈频率进行分组
            for (ZXGL_XiangMuLiXiang_MingXiEntity mingXiEntity : fankui) {
                ZXGL_XiangMuFanKuiEntity entity = zxglXiangMuFanKuiService.addProjectFk(mingXiEntity.getId());

                UserEntity userEntity = usersApi.getInfoById(entity.getZrr());
                if (userEntity != null) {
                    UserInfo userInfo = userService.copyFromEntity(userEntity);
                    entity.setOrganizeName(userInfo.getOrganizeName());
                    entity.setOrganizeId(userInfo.getOrganizeId());
                    entity.setCreatorUser(userInfo.getUserName());
                    entity.setCreatorUserId(userInfo.getUserId());
                    entity.setCompanyId(userInfo.getCompanyId());
                    entity.setCompanyName(userInfo.getCompanyName());
                    entity.setDepartmentId(userInfo.getDepartmentId());
                    entity.setDepartmentName(userInfo.getDepartmentName());
                    entity.setCreatorTime(new Date());
                    //生成项目反馈
                    entity.setId(RandomUtil.uuId());
                    zxgl_xiangMuFanKuiService.add(entity);
                }

                /**
                 * 任务反馈
                 */
                //获取任务记录
                List<ZXGL_XiangMuFenJie_mingxibiaoEntity> mingxibiaolist = zxglXiangMuFenJieMingxibiaoService.getListByXmId(mingXiEntity.getId());
                for (ZXGL_XiangMuFenJie_mingxibiaoEntity mx : mingxibiaolist) {
                    ZXGL_RenWuFanKuiEntity mingxibiao = zxglXiangMuFenJieMingxibiaoService.createEntity(mx.getId());
                    userEntity = usersApi.getInfoById(mingxibiao.getFkr());
                    if (userEntity != null) {
                        UserInfo userInfo = userService.copyFromEntity(userEntity);
                        mingxibiao.setOrganizeName(userInfo.getOrganizeName());
                        mingxibiao.setOrganizeId(userInfo.getOrganizeId());
                        mingxibiao.setCreatorUser(userInfo.getUserName());
                        mingxibiao.setCreatorUserId(userInfo.getUserId());
                        mingxibiao.setCompanyId(userInfo.getCompanyId());
                        mingxibiao.setCompanyName(userInfo.getCompanyName());
                        mingxibiao.setDepartmentId(userInfo.getDepartmentId());
                        mingxibiao.setDepartmentName(userInfo.getDepartmentName());
                        mingxibiao.setCreatorTime(new Date());
                        //生成项目反馈
                        mingxibiao.setId(RandomUtil.uuId());
                        zxgl_renWuFanKuiService.add(mingxibiao);
                    }
                }
            }
        }
    }

}
